﻿using System.Text;
using System;
namespace JCW.Common
{
    public class PagingBuilder
    {
        // Fields
        private StringBuilder _TxtBuilder = new StringBuilder();

        // Methods
        private static string BuildCountPagingBar(PagingInfoEntity pagingInfo, string clientCallBack, int itemCount)
        {
            StringBuilder builder = new StringBuilder();
            int num = itemCount;
            int currentPageIndex = pagingInfo.CurrentPageIndex;
            int pageTotal = pagingInfo.PageTotal;
            if (currentPageIndex > pageTotal)
            {
                currentPageIndex = pageTotal;
            }
            int num4 = (pagingInfo.PageTotal > num) ? num : pagingInfo.PageTotal;
            if (currentPageIndex > 1)
            {
                builder.Append(string.Format("<span title=\"第一页\"><a href=\"javascript:{0}({1})\" >首页</a></span>&nbsp;", clientCallBack, 1));
                builder.Append(string.Format("<span title=\"上一页\"><a href=\"javascript:{0}({1})\" >上一页</a></span>&nbsp;", clientCallBack, currentPageIndex - 1));
            }
            else
            {
                builder.Append("<span title=\"第一页\">首页</span>&nbsp;");
                builder.Append("<span title=\"第一页\">上一页</span>&nbsp;");
            }
            int num5 = 0;
            int num6 = ((num % 2) == 0) ? (num / 2) : ((num / 2) + 1);
            if ((pageTotal > num) && (currentPageIndex > num6))
            {
                if ((currentPageIndex % num6) == 0)
                {
                    num5 = currentPageIndex - num6;
                }
                else
                {
                    num5 = currentPageIndex - num6;
                }
                if ((currentPageIndex <= pageTotal) && (currentPageIndex > (pageTotal - itemCount)))
                {
                    num5 = ((pageTotal - itemCount) > 0) ? (pageTotal - itemCount) : 1;
                }
            }
            for (int i = 1; i <= num4; i++)
            {
                num5++;
                if (num5 > pageTotal)
                {
                    break;
                }
                if (num5 == currentPageIndex)
                {
                    builder.Append(string.Format("<span class=\"curpage\" style=\"color:red\"><a href=\"javascript:{0}({1})\" >{1}</a></span> ", clientCallBack, num5));
                }
                else
                {
                    builder.Append(string.Format("<span><a href=\"javascript:{0}({1})\" >{1}</a></span> ", clientCallBack, num5));
                }
            }
            if (currentPageIndex < pageTotal)
            {
                builder.Append(string.Format(" <span><a href=\"javascript:{0}({1})\" >下一页</a></span>&nbsp;<span title=\"最后一页\"><a href=\"javascript:{0}({2})\" >尾页</a></span>", clientCallBack, currentPageIndex + 1, pageTotal));
            }
            else
            {
                builder.Append("<span>下一页</span>&nbsp;");
                builder.Append("<span>尾页</span>");
            }
            string str = string.Format("<span style='display:none'>总记录数：{0}</span> <span>{1}/{2}</span>", pagingInfo.TotalRecord, pagingInfo.CurrentPageIndex, pagingInfo.PageTotal);
            // builder.Append(string.Concat(new object[] { "&nbsp;<span style=\"padding:0px; margin:0px;line-height:100%;\"><input type=\"text\" style=\"width:30px;\" onkeyup=\"numonly(this); \" onkeydown=\"if(event.keyCode==13) { if(this.value>0){ if(parseInt(this.value)<=", pageTotal, "){ ", clientCallBack, "(this.value);} else {", clientCallBack, "(", pageTotal, ");}  } return false;}\" /></span>" }));
            return string.Format("<div class=\"paging\">{0} {1}</div>", str, builder.ToString());
        }

        public static string CreateCommonPagingBar(PagingInfoEntity pagingInfo, string clientCallBack)
        {
            StringBuilder builder = new StringBuilder();
            int currentPageIndex = pagingInfo.CurrentPageIndex;
            int pageTotal = pagingInfo.PageTotal;
            int totalRecord = pagingInfo.TotalRecord;
            int pageSize = pagingInfo.PageSize;
            if (currentPageIndex > pageTotal)
            {
                currentPageIndex = pageTotal;
            }
            builder.Append(string.Format("<span>页次:{0}/{1}页 共{2}条记录 {3}条记录/页</span>&nbsp;&nbsp;&nbsp;&nbsp;", new object[] { currentPageIndex, pageTotal, totalRecord, pageSize }));
            if ((currentPageIndex > 1) && (currentPageIndex < pageTotal))
            {
                builder.Append(string.Format("<a href=\"javascript:{0}({1})\">第一页</a> ", clientCallBack, 1));
                builder.Append(string.Format("<a href=\"javascript:{0}({1})\">上一页</a> ", clientCallBack, currentPageIndex - 1));
                builder.Append(string.Format("<a href=\"javascript:{0}({1})\">下一页</a> ", clientCallBack, currentPageIndex + 1));
                builder.Append(string.Format("<a href=\"javascript:{0}({1})\">尾页</a> ", clientCallBack, pageTotal));
            }
            else if ((currentPageIndex == 1) && (pageTotal > 1))
            {
                builder.Append("第一页 ");
                builder.Append("上一页 ");
                builder.Append(string.Format("<a href=\"javascript:{0}({1})\">下一页</a> ", clientCallBack, currentPageIndex + 1));
                builder.Append(string.Format("<a href=\"javascript:{0}({1})\">尾页</a> ", clientCallBack, pageTotal));
            }
            else if ((currentPageIndex == 1) && (pageTotal == 1))
            {
                builder.Append("第一页 ");
                builder.Append("上一页 ");
                builder.Append("下一页 ");
                builder.Append("尾页 ");
            }
            else
            {
                builder.Append(string.Format("<a href=\"javascript:{0}({1})\">第一页</a> ", clientCallBack, 1));
                builder.Append(string.Format("<a href=\"javascript:{0}({1})\">上一页</a> ", clientCallBack, currentPageIndex - 1));
                builder.Append("下一页 ");
                builder.Append("尾页 ");
            }
            string str = Guid.NewGuid().ToString();
            builder.Append(string.Format("转到<span><input id=\"Go_{0}\" type=\"text\" style=\"width:35px; \" value=\"{2}\"> <a href=\"javascript:void(0);\" onclick=\"var page=document.getElementById('Go_{0}').value;{1}(page);\">GO</a></span>", str, clientCallBack, currentPageIndex));
            return string.Format("<div style=\"text-align:center\">{0}</div>", builder.ToString());
        }

        public static string CreateCommonPagingBar(int curPageIndex, int pageSize, int totalRecord, string clientCallBack)
        {
            return CreateCommonPagingBar(new PagingInfoEntity(curPageIndex, pageSize, totalRecord), clientCallBack);
        }

        public static string CreatePagingBar(PagingInfoEntity pagingInfo, string clientCallBack)
        {
            return BuildCountPagingBar(pagingInfo, clientCallBack, 10);
        }

        public static string CreatePagingBar(PagingInfoEntity pagingInfo, string clientCallBack, int itemCount)
        {
            return BuildCountPagingBar(pagingInfo, clientCallBack, itemCount);
        }

        public static string CreatePagingBar(int curPageIndex, int pageSize, int totalRecord, string clientCallBack)
        {
            if ((totalRecord % pageSize) != 0)
            {
                int num1 = totalRecord / pageSize;
            }
            else
            {
                int num2 = totalRecord / pageSize;
            }
            return BuildCountPagingBar(new PagingInfoEntity(curPageIndex, pageSize, totalRecord), clientCallBack, 10);
        }

        public static string CreatePagingBar(int curPageIndex, int pageSize, int totalRecord, string clientCallBack, int itemCount)
        {
            if ((totalRecord % pageSize) != 0)
            {
                int num1 = totalRecord / pageSize;
            }
            else
            {
                int num2 = totalRecord / pageSize;
            }
            return BuildCountPagingBar(new PagingInfoEntity(curPageIndex, pageSize, totalRecord), clientCallBack, itemCount);
        }

        // Nested Types
        private enum PagingType
        {
            Default = 2,
            Normal = 1
        }
    }



    public class PagingInfoEntity
    {
        // Fields
        private string _ClientCallBack;
        private int _CurrentPageIndex;
        private int _ItemRecord;
        private int _PageSize;
        private int _TotalRecord;
        private PagingType _Type;

        // Methods
        public PagingInfoEntity()
        {
            this._Type = PagingType.PagingCode;
        }

        public PagingInfoEntity(int curPageIndex, int pageSize, int totalRecord)
        {
            this._Type = PagingType.PagingCode;
            this._CurrentPageIndex = curPageIndex;
            this._PageSize = pageSize;
            this._TotalRecord = totalRecord;
        }

        public PagingInfoEntity(int curPageIndex, int pageSize, int totalRecord, PagingType pagingType)
        {
            this._Type = PagingType.PagingCode;
            this._CurrentPageIndex = curPageIndex;
            this._PageSize = pageSize;
            this._TotalRecord = totalRecord;
            this._Type = pagingType;
        }

        public PagingInfoEntity(int curPageIndex, int pageSize, int totalRecord, string clientCallBack)
        {
            this._Type = PagingType.PagingCode;
            this._CurrentPageIndex = curPageIndex;
            this._PageSize = pageSize;
            this._TotalRecord = totalRecord;
            this._ClientCallBack = clientCallBack;
        }

        public PagingInfoEntity(int curPageIndex, int pageSize, int totalRecord, string clientCallBack, PagingType pagingType)
        {
            this._Type = PagingType.PagingCode;
            this._CurrentPageIndex = curPageIndex;
            this._PageSize = pageSize;
            this._TotalRecord = totalRecord;
            this._ClientCallBack = clientCallBack;
            this._Type = pagingType;
        }

        public PagingInfoEntity(int curPageIndex, int pageSize, int totalRecord, string clientCallBack, PagingType pagingType, int itemRecord)
        {
            this._Type = PagingType.PagingCode;
            this._CurrentPageIndex = curPageIndex;
            this._PageSize = pageSize;
            this._TotalRecord = totalRecord;
            this._ClientCallBack = clientCallBack;
            this._Type = pagingType;
            this._ItemRecord = itemRecord;
        }

        // Properties
        public string ClientCallBack
        {
            get
            {
                return this._ClientCallBack;
            }
            set
            {
                this._ClientCallBack = value;
            }
        }

        public int CurrentPageIndex
        {
            get
            {
                return this._CurrentPageIndex;
            }
            set
            {
                this._CurrentPageIndex = value;
            }
        }

        internal int ItemRecord
        {
            get
            {
                return this._ItemRecord;
            }
            set
            {
                this._ItemRecord = value;
            }
        }

        public int PageSize
        {
            get
            {
                return this._PageSize;
            }
            set
            {
                this._PageSize = value;
            }
        }

        public int PageTotal
        {
            get
            {
                if ((this._TotalRecord % this._PageSize) != 0)
                {
                    return ((this._TotalRecord / this._PageSize) + 1);
                }
                return (this._TotalRecord / this._PageSize);
            }
        }

        public int TotalRecord
        {
            get
            {
                return this._TotalRecord;
            }
            set
            {
                this._TotalRecord = value;
            }
        }

        public PagingType Type
        {
            get
            {
                return this._Type;
            }
            set
            {
                this._Type = value;
            }
        }

        // Nested Types
        public enum PagingType
        {
            Common,
            PagingCode
        }
    }
}